/**
 * related to caseInfo.ui
 * 
 * @Author : ln008
 * @Timestamp : 2017-04-19
 */
var nf = sm("do_Notification");
var app = sm("do_App");
var page = sm("do_Page");
var dialog = sm("do_Dialog");
var server = require('config/server')
var Controller = require("core/controller");
var log = require("util/log");
var net = require("util/net");
var navigate = require("util/navigate");
var router = require("config/router")
var filter = require('util/filter')
var el = ui("exlv");
var btn_overPage = ui("btn_overPage");
var btn_earlyPage = ui("btn_earlyPage");
var common = require('util/common')
Controller({
	name : 'index',
	mapping : {
		"exlv" : [ "data1", "data2" ]
	},
	data : {
		data1 : [
		/**
		 * 给数组模板添加数据source://view/do_ExpandableListView/test2.ui
		 */
		{
			template : 0,
			"productName" : "",
			"overDueSumAmount" : "",
			"cupoEarlymoney" : "",
			"promiseAmt" : "",
			"promiseDate" : "",
			"cupoCasenum" : "",
			"cupoBatch" : "",
			"contractAmount" : "",
			// "case_hmdate" : "",
			// "loanDate" : "",
			"cupoPrincipal" : ""
		}, {
			template : 1,
			"payaStatus" : "",
			"createTime" : "",
//			"payaApprovedate" : "",
			"payaEmpName" : "",
			"cas_icon" : "",
			"layout_csTime" : ""
		} ],
		data2 : [
		/**
		 * 给数组模板添加数据
		 */
		[ {
			template : 0,
		} ], [ {
			template : 1,
			"payaPayNum" : "",
			"payaDelaynum" : "",
			"payaType" : "",
			"payaStyle" : "",
			"case_revok":""
		} ] ],
		ovlist : {},
		newCaseList : {},
		promisList : {},
		cupoOrdernum : page.getData().cupoOrdernum
	},
	ui : {
		root : '$',
		exlv:'exlv'
	},
	event : {
		'$page' : {
			"casRefsh" : function() {	
				this.data.data1 = [{}]
				this.data.data2 = [{}]
				this.getCaseList()	
			},scope : this
			,
			"repDetail" : function() {
				this.toRepayDetail(this.data.caseBaseData.current)
			}
		},
		'btn_overPage' : {
			"touch" : function() {
				this.toOverPage(this.data.caseBaseData.current)
			}
		},
		'btn_earlyPage' : {
			"touch" : function() {
				this.toEarlyPage(this.data.caseBaseData.current)
			}
		},
		'exlv' : {
			"groupTouch" : function(index) {
				var data = this.data.data1[index]
				data.cas_icon = data.cas_icon === "e6aa" ? "e6ac" : "e6aa"
				data.layout_csTime = data.layout_csTime === false ? true: false
				this.getSource('exlv')[0].updateOne(index, data)
			}
		}

	},
	methods : {
		toOverPage : function(cop) {
			navigate.redirectTo({
				view : router.case.overPay,
				data : {
					cop : cop,
					cupoOrdernum : this.data.caseBaseData.current.cupoOrdernum
				},
				back : function(data) {
					this.getCaseList()
				},
				scope : this
			})
		},
		toEarlyPage : function(ear) {
			navigate.redirectTo({
				view : router.case.earlyPay,
				data : {
					ear : ear,
					cupoOrdernum : this.data.caseBaseData.current.cupoOrdernum
				},
				back : function(data) {
					this.getCaseList()
				},
				scope : this
			})
		},
		toRepayDetail : function(repd) {
			navigate.redirectTo({
				view : router.case.repayDetail,
				data : {
					orderNum : repd
				},
				back : function(data) {
					232323
				}
			})
		},
		getCaseBaseData : function(data) {
			data.cupoCasenum = this.data.caseBaseData.current.cupoCasenum||''
			data.cupoBatch = this.data.caseBaseData.current.cupoBatch||''
			data.cupoPrincipal = this.data.caseBaseData.current.cupoPrincipal||''
			// data.cupoCasenum=this.data.caseBaseData.current.cupoCasenum
			data.cupoCasenum = this.data.caseBaseData.current.cupoCasenum||''
			data.cupoEarlymoney = this.data.caseBaseData.current.cupoEarlymoney
			// "promiseAmt" : "",
			// "case_pmdate" : "",
			data.promiseAmt = this.data.promisList.promiseAmt || ''
			data.promiseDate = this.data.promisList.promiseDate || ''
			data.showPromise = data.promiseAmt!==''
			this.data.data1[0] = data
			this.data.data2[0] = [ {
				template : 0
			} ]

		},
		getCaseList : function(csparGrand) {
			this.data.data1 = [{}]
			this.data.data2 = [{}]
			// 添加案件基本信息
			this.getCasePromise(this.data.caseBaseData.current)
			return net.send({
				server : server.organization.caseGrandInfo,
				data : {
					cupoOrdernum : this.data.caseBaseData.current.cupoOrdernum
				},
				success : function(csgrandata) {
//					log.info(11414,filter.toThousands(1888888888))
					this.data.ovlist = csgrandata.orderNum
					this.getData1()
					this.getCaseBaseData(csgrandata)
				},
				scope : this
			})

		},
		getCasePromise : function(csProm) {
			return net.send({
				server : server.organization.casePayPromise,
				data : {
					cupoCaseNum : csProm.cupoCasenum
				},
				success : function(csData) {
					this.data.promisList = csData
				},
				error : function(data) {
				},
				scope : this
			})
		},
		getData1 : function() {
			return net.send({
				server : server.organization.reduceApplyState,
				data : {
					cupoId : this.data.caseBaseData.current.cupoId,
					sort: "createTime,desc"
				},
				success : function(casedata) {
					this.data.ovlist = casedata.content[0];
					var titleData = casedata.content.map(function(title) {
						return {
							payaStatus : filter.typeCodeConvert(title.payaStatus),
//							payaApplyDate : title.payaApplyDate,
							createTime : common.dateFormat(title.createTime,'yyyy-MM-dd hh:mm:ss')||'',
							payaEmpName : title.payaEmpName,
							cas_icon : "e6ac",
							layout_csTime : false,
							template : 1
						}
					})
					var listData = casedata.content.map(function(cont) {
						var payaStatusRenge = [66,64,52]
						var sta=payaStatusRenge.includes(cont.payaStatus)?"#0191DAFF":"A9A9A9FF"
						return [ {
							payaPayNum : cont.payaPayNum,
							payaDelaynum : cont.payaDelaynum,
							payaType : filter.typeCodeConvert(cont.payaType),
							payaStyle : filter.typeCodeConvert(cont.payaStyle),
							caseIconId : cont.payaId,
							idStatus : cont.payaStatus,
							case_revok:sta,
							template : 1,
						} ]
					})
					this.data.data1.push.apply(this.data.data1, titleData)
					log.info(678,this.data.data1)
					this.data.data2.push.apply(this.data.data2, listData)
					this.refresh('exlv')
				},
				scope : this
			})
		},

	},
	loaded : function(data) {
		this.data.caseBaseData = data
//		promise.chain([
//getCasePromise,
//getData1
//		               ]).then(app)
//		               
//		promise.join([
//			getCasePromise(),
//			getData1()
//		 ]).then()
//		
//		 getCasePromise.then(fucntion())
		this.loadReady([
		                this.getCaseList()
		                ])
		
	}
})